/**基础配置*/
var DOMAIN = 'www.genetuan.com';
//var DOMAIN = 'www.blovet.top';
//var DOMAIN = '192.168.31.119';
//  var DOMAIN = '192.168.31.220';
//    var DOMAIN = '192.16  8.0.103';
var PATH = '/';
var EXPIRES = 1;//cookie过期时间
var ipCookiesParam = {expires: EXPIRES, domain: DOMAIN, path: PATH};

var HOST = 'http://www.genetuan.com/cms';
// var HOST = 'http://192.168.31.194:8080/cms';
var ALISRC = 'http://genetuan.oss-cn-shenzhen.aliyuncs.com/';
var statusCODE = {
    OK: 2000,//成功
    NOTLOGIN: 3002,//未登录
};

var yjx = angular.module("myapp", [
    //'ngRoute',
    'ui.router',
    //'ngTouch',
    'ipCookie',
    //'swipe',  //滑动模块
    'mobile-angular-ui',
    // touch/drag feature: this is from 'mobile-angular-ui.gestures.js'
    // it is at a very beginning stage, so please be careful if you like to use
    // in production. This is intended to provide a flexible, integrated and and
    // easy to use alternative to other 3rd party libs like hammer.js, with the
    // final pourpose to integrate gestures into default ui interactions like
    // opening sidebars, turning switches on/off ..
    //'mobile-angular-ui.gestures',
    'ui.bootstrap',
]);

yjx.run(['$rootScope', '$window', '$location', '$log', 'ipCookie', '$timeout', 'browserJudge', 'LoginService', '$init', '$fetch','getRemRatioService','urlCgService','modalService',
    function ($rootScope, $window, $location, $log, ipCookie, $timeout, browserJudge, LoginService, $init, $fetch,getRemRatioService,urlCgService,modalService) {
        //浏览器判断
        //alert("浏览器信息：\n"+browserJudge.userAgent())
        //alert("是否为单窗口："+browserJudge.isSingleWindow());
        //browserJudge.changeDevice('mobile');//判断设备 定向到相应版本
        //getRemRatioService.getRios();//获取rem与px的转换率

        if(browserJudge.isWx()){
                if(browserJudge.userAgent().match('android')){
                    $rootScope.isAndroid = true
                }else{
                    $rootScope.isAndroid = false
                }
        }else{
            if($location.path()=='/productDetail' && $location.search() && $location.search().pdfUrl){

            }else{
                modalService.mainMsgError("请在微信浏览器打开")
            }
        }

        function goLogin() {
            if(window.location.href.search(/\#/)>0){
                /*打开ios下微信转发url的处理*/
                var i = window.location.href.search(/\?/);
                var i2 = window.location.href.search(/\#/);
                if(i>0 && i2>0 && i<i2){
                    var absurl2 = window.location.href.split('#/')[1]
                    if(absurl2){
                        window.location.href = 'http://www.genetuan.com/#/'+absurl2;
                        LoginService.wxAutoLogin(absurl2);
                    }else{
                        window.location.href = 'http://www.genetuan.com/#/';
                        LoginService.wxAutoLogin();
                    }
                }else{
                    if($location.url() != "/about"){
                        LoginService.wxAutoLogin($location.url());
                    }
                }
            }else{
                var absurl =  window.location.href.split('://')[1]
                var url = absurl.split('/')[1]
                /*打开android下微信转发url的处理*/
                //android分享出去的内容url以参数的形式 解决不带#出现404的问题
                /*　 朋友圈   from=timeline&isappinstalled=0
                 　　微信群   from=groupmessage&isappinstalled=0
                 　　好友分享 from=singlemessage&isappinstalled=0*/
                //alert(url)
                if(url.match('url=')){
                    var index = url.substring(url.search(/url\=/),url.search(/&/));
                    //console.log(index);
                    var index2 = index.substring(index.search(/\=/)+1);
                    //alert(index2);
                    url = '?'+url.substring(url.search(/&/)+1)
                    if(absurl.match('from=singlemessage') || absurl.match('from=groupmessage') || absurl.match('from=timeline') ){
                        /*from=singlemessage 是转发出去后微信自动在url上添加的内容  用这种方式处理限制自带参数不能有from字段*/
                        //var i = url.search(/\?/);
                        var i2 = url.search(/from/);
                        var hash = url.substring(0,i2-1)
                        window.location.href = 'http://www.genetuan.com/#/'+index2+hash;
                        LoginService.wxAutoLogin(index2+hash);
                    }else{
                        window.location.href = 'http://www.genetuan.com/#/'+index2+url;
                        LoginService.wxAutoLogin(index2+url);
                    }
                }else{
                    if(url != "/about"){
                        LoginService.wxAutoLogin(url);
                    }
                }
            }
        }

        if(browserJudge.isWx()){
            goLogin()
        }else{
            testLogin()
        }

        $rootScope.defaultHeadPic = "common/img/user/headPic-2.png";
        $rootScope.servicePic = "common/img/user/servicePic.png";
        
        /*路由tab变量设置以下*/
        $rootScope.tabCtrl = {
            tab0: true,
            tab1: false,
            tab2: false,
            tab3: false,
        }
        $rootScope.tabCtrlNameArr = ['home', 'product', 'support', 'user'];
        $rootScope.tabCtrlName = $rootScope.tabCtrlNameArr[0];
        /*路由tab变量设置以上*/
         function testLogin(){
             $rootScope.identity = 1; //非微信浏览器测试时用
             $rootScope.userId = 2;//非微信浏览器测试时用
             $rootScope.user = {
                 userId:2,
                 nickname:'wanglan',
                 avatar:'http://wx.qlogo.cn/mmopen/vi_32/ibytib2JRtibvnkl1iaaKLL3l5TtczMNicMtAVic0k2WRVDTmUzNldSVeVGGCbOoG4E1XnzA6Mich3FL5L5PcLsRAibibJA/0',
                 digests:'bababa',
                 userType:1,
             };
         }
        //url、路由监听
        var locationChangeStartOff = $rootScope.$on('$locationChangeStart', locationChangeStart);
        var locationChangeSuccessOff = $rootScope.$on('$locationChangeSuccess', locationChangeSuccess);

        /*ui-Route*/
        var stateChangeStartOff = $rootScope.$on('$stateChangeStart', stateChangeStart);
        var stateChangeSuccessOff = $rootScope.$on('$stateChangeSuccess', stateChangeSuccess);

        function locationChangeStart(event) {

        }

        function locationChangeSuccess(event) {
            //console.log("locationChangeSuccess..." + $location.url())
            /*更新全局path*/
            //console.log(event)
            urlCgService.watch();
        }

        /*ui-Route*/
        function stateChangeStart(event) {
        }

        function stateChangeSuccess(event) {
        }

         document.body.addEventListener('touchmove', function(evt) {
            //In this case, the default behavior is scrolling the body, which
            //would result in an overflow.  Since we don't want that, we preventDefault.
            if(!evt._isScroller) {
                //alert(evt._isScroller)
                evt.preventDefault();
            }
        },false);
    }

]);
